﻿using Yorrick.FlickrMetadataSynchr.Local;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Yorrick.FlickrMetadataSynchr.Data;
using Yorrick.FlickrMetadataSynchr.Events;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Yorrick.FlickrMetadataSynchr.UnitTests
{
    /// <summary>
    ///This is a test class for LocalPicturesHelperTest and is intended
    ///to contain all LocalPicturesHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LocalPicturesHelperTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        /// Adds tags based on the image file name to the metadata for a local picture file. E.g., IMG_4312_Stitch.jpg will
        /// be tagged with IMG_4312 and Stitch.
        /// </summary>
        /// <remarks>This is not really a test method. It just uses the facilities of Visual Studio to get
        /// an easy process host and easy method selection for running an action.</remarks>
        [TestMethod]
        public void AddImgFilenameAsTag()
        {
            LocalPicturesHelper helper = new LocalPicturesHelper();

            string localFolder = @"C:\Users\xyz\Pictures\xyz";

            IList<string> fileNames = helper.GetPictureFileNames(localFolder, false);

            IList<LocalPictureMetadata> allMetadata = helper.GetSyncMetadata(fileNames, null);

            AddFileNameTagToMetadata(allMetadata);

            helper.SimulateUpdatingMetadata = false;

            helper.UpdatePictureMetadata(allMetadata, null);
        }

        /// <summary>
        /// Helper method that adds tags based on the image file name.
        /// </summary>
        /// <param name="allMetadata">Metadata for all files that should be processed.</param>
        private void AddFileNameTagToMetadata(IList<LocalPictureMetadata> allMetadata)
        {
            Regex regex = new Regex("^(?<tag>IMG_[0-9]{4}).*$", RegexOptions.Singleline);

            foreach (LocalPictureMetadata metadata in allMetadata)
            {
                if (metadata.FileNameWithoutExtension.Contains("Stitch"))
                {
                    AddTag("Stitch", metadata);
                }

                var match = regex.Match(metadata.FileNameWithoutExtension);

                if (!match.Success)
                {
                    continue;
                }

                string fileNameTag = match.Groups["tag"].Captures[0].Value;

                AddTag(fileNameTag, metadata);
            }
        }

        private void AddTag(string tagToAdd, LocalPictureMetadata metadata)
        {
            foreach (string tag in metadata.Tags)
            {
                if (tag == tagToAdd)
                {
                    Trace.WriteLine("Skipping file which already has tag " + tagToAdd);
                    continue;
                }
            }

            List<string> tags = new List<string>(metadata.Tags.Length + 1);
            tags.InsertRange(0, metadata.Tags);
            tags.Add(tagToAdd);

            metadata.Tags = tags.ToArray();

            Trace.WriteLine("Added tag " + tagToAdd + " to " + metadata.FilePath);
        }
    }
}
